// pages/Home/exam/exam.js
var app = getApp()
var call = require("../../../utils/request.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        "id": '',
        "question": "",
        "option1": "",
        "option2": "",
        "option3": "",
        "option4": "",
        "rightOption": "",
        "buttonColour1":"bg-gray",
        "buttonColour2":"bg-gray",
        "buttonColour3":"bg-gray",
        "buttonColour4":"bg-gray",
        "favorite":"收藏",
        "isCheck":0//是否已选择
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        call.getData('/question-bank/get-one',this.shuffleSuc, this.fail);
    },
    shuffleSuc: function (data) {
        this.setData({
            id : data.data.id,
            question : data.data.question,
            option1 : data.data.option1,
            option2 : data.data.option2,
            option3 : data.data.option3,
            option4 : data.data.option4,
            rightOption : data.data.rightOption,
        })
    },
    fail: function () {
        console.log("失败")
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    next: function (e) {
        call.getData('/question-bank/get-one',this.shuffleSuc, this.fail);
        this.setData({
            buttonColour1 : "bg-gray",
            buttonColour2 : "bg-gray",
            buttonColour3 : "bg-gray",
            buttonColour4 : "bg-gray",
            favorite:"收藏",
            isCheck:0
        })
    },
    optionButton:function(e){
        if(this.data.isCheck!=0){
            return;
        }
        this.data.isCheck = 1;
        var id = e.currentTarget.id;
        var buttonId = "buttonColour"+id;
        if(this.data.rightOption==id){
            this.setData({
                [buttonId] : "bg-green",
            })
            //提交做题记录
            call.request('/exercise-record/create',{"questionId":this.data.id,"option":0});
        }else{
            this.setData({
                [buttonId] : "bg-red",
                // buttonColour2 : "bg-red",
                // buttonColour3 : "bg-red",
                // buttonColour4 : "bg-red"
            })
            var reghtButton = "buttonColour"+this.data.rightOption;
            this.setData({
                [reghtButton] : "bg-green",
            })
            //提交做题记录
            call.request('/exercise-record/create',{"questionId":this.data.id,"option":id});
        }
        
    },
    btnAddFavorite: function (e) {
        if(this.data.favorite=="收藏"){
            call.request('/favorite/create',{"questionId":this.data.id},this.addSuc);
        }else{
            call.request('/favorite/delete',{"questionId":this.data.id},this.addSuc);
        }
    },
    addSuc: function (data) {
        if(this.data.favorite=="收藏"){
            this.setData({
                favorite : "取消收藏",
            })
        }else{
            this.setData({
                favorite : "收藏",
            })
        }
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})